package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Lugar;


public class LugarDAO implements ILugarDAO {
    private static final Log log = LogFactory.getLog(LugarDAO.class);

    // property constants
    //public static final Long  LUGACODDANEN = "lugacoddanen";
    public static final String LUGACODDANEN = "lugacoddanen";

    //public static final String  LUGADESCRIPV = "lugadescripv";
    public static final String LUGADESCRIPV = "lugadescripv";

    //public static final Integer  LUGAIDN = "lugaidn";
    public static final String LUGAIDN = "lugaidn";
    
    public static final String LUGAR = "lug_lugaidn";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Lugar Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Lugar instance) {
        log.debug("saving Lugar instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Lugar Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Lugar instance) {
        log.debug("deleting Lugar instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Lugar Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Lugar instance) {
        log.debug("updating Lugar instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Lugar findById(Integer id) {
        log.debug("finding Lugar instance with id: " + id);

        try {
            Lugar instance = (Lugar) getSession().get(Lugar.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Lugar failed", re);
            throw re;
        }
    }

    public List<Lugar> findByExample(Lugar instance) {
        log.debug("finding Lugar instance by example");

        try {
            List results = getSession().createCriteria("Lugar")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Lugar entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Lugar> found by query
        */
    public List<Lugar> findByProperty(String propertyName, Object value) {
        log.debug("finding Lugar instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Lugar as model where model." +
                propertyName +"= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }
    
    public List<Lugar> findByLug_lugaidn(Object lugaidn) {
       
    	 try {
	            String queryString = "from Lugar where lugar.lugaidn = ? order by lugadescripv asc";
	            Query queryObject = getSession().createQuery(queryString);
	            queryObject.setParameter(0, lugaidn);
	            
	            return queryObject.list();
	        } catch (RuntimeException re) {
	            log.error("find Dptos failed", re);
	            throw re;
	        }
    	

    }

    public List<Lugar> findByLugacoddanen(Object lugacoddanen) {
        return findByProperty(LUGACODDANEN, lugacoddanen);
    }

    public List<Lugar> findByLugadescripv(Object lugadescripv) {
        return findByProperty(LUGADESCRIPV, lugadescripv);
    }

    public List<Lugar> findByLugaidn(Object lugaidn) {
        return findByProperty(LUGAIDN, lugaidn);
    }

    /**
    * Find all Lugar entities.
    *
    * @return List<Lugar> all Lugar instances
    */
    public List<Lugar> findAll() {
        log.debug("finding all Lugar instances");

        try {
            String queryString = "from Lugar order by lugadescripv asc";
            Query queryObject = getSession().createQuery(queryString);

            System.out.println(queryObject.list().get(0).toString());
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }
    
    
    public List<Lugar> findLugarporCodigo(Long codigoLugar) {
        log.debug("finding all Lugar instances");

        try {
            String queryString = "from Lugar where lugacoddanen="+codigoLugar;
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Lugar> findByCriteria(String whereCondition) {
        log.debug("finding Lugar " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Lugar model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Lugar> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Lugar failed", re);
            throw re;
        }
    }

    public List<Lugar> findPageLugar(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Lugar findPageLugar");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Lugar model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Lugar model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberLugar() {
        log.debug("finding Lugar count");

        try {
            String queryString = "select count(*) from Lugar model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }

    //consulta los departamentos
	public List<Lugar> getDptos() {
		 log.debug("finding Dptos Lugar instances");

	        try {
	            String queryString = "from Lugar where lugar is null order by lugadescripv asc";
	            Query queryObject = getSession().createQuery(queryString);

	            return queryObject.list();
	        } catch (RuntimeException re) {
	            log.error("find Dptos failed", re);
	            throw re;
	        }
	}
	
}
